﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.IO;

namespace Zombie_Demo.Utilities
{
    public static class ContentLibrary
    {
        private static Dictionary<string, Texture2D> textures;
        private static Dictionary<string, string> xmlFilePaths;
        private static Dictionary<string, SpriteFont> fonts;
        private static Microsoft.Xna.Framework.Content.ContentManager Content;
        private static string contentFilePath;

        public static string ContentFilePath { get { return contentFilePath; } }

        public static void Init(Game1 game)
        {
            textures = new Dictionary<string, Texture2D>();
            xmlFilePaths = new Dictionary<string, string>();
            fonts = new Dictionary<string, SpriteFont>();
            setDirectory();
        }

        public static void SetContent(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            Content = content;
            Content.RootDirectory = "Content";
        }

         /// <summary>
         /// Load a texture into memory.
         /// </summary>
         /// <param name="key">Name to call the texture in memory.</param>
         /// <param name="filename">Name of the texture on the hard disk.</param>
         /// <returns>True if the file was loaded sucessfully.</returns>
        public static bool LoadTexture(string key, string filename)
        {
            try{
                textures.Add(key, Content.Load<Texture2D>("Textures\\" + filename));
                return true;
            }
            catch (Exception e)
            {
                Utilities.ErrorManager.AddError(e);
                return false;
            }
        }

         /// <summary>
         /// Return a texture from memory.
         /// </summary>
         /// <param name="key">The name of the texture saved using the LoadTexture function.</param>
         /// <returns>The requested texture if it is in memory, otherwise the error texture.</returns>
        public static Texture2D GetTexture(string key)
        {
            if (textures.ContainsKey(key))
                return textures[key];
            else
            {
                bool didLoad = LoadTexture(key, key);
                if (didLoad)
                    return textures[key];
                else
                    return textures["Error"];
            }
        }

         /// <summary>
         /// Load an XML file into memory.
         /// </summary>
         /// <param name="key">The name to save the file under in memory.</param>
         /// <param name="filePath">The name of the file on the hard drive.</param>
         /// <returns>True if the load was sucessful.</returns>
        public static bool LoadXMLFile(string key, string filePath)
        {
             try
             {
                  xmlFilePaths.Add(key, contentFilePath + "XML\\" + filePath);
                  return true;
             }
             catch (Exception e)
             {
                  Utilities.ErrorManager.AddError(e);
                  return false;
             }
        }

         /// <summary>
         /// Retrieve the string of the file, ready to deserialize.
         /// </summary>
         /// <param name="key">The name of the file as specified by the LoadXMLFile function.</param>
         /// <returns>String of the file that was requested, else return the error file.</returns>
        public static string GetXMLFile(string key)
        {
             if (xmlFilePaths.ContainsKey(key))
             {
                  StreamReader reader = new StreamReader(xmlFilePaths[key] + ".xml");
                  return reader.ReadToEnd();
             }
             else
             {
                  bool didLoad = LoadXMLFile(key, key);
                  if (didLoad)
                  {
                       StreamReader reader = new StreamReader(xmlFilePaths[key] + ".xml");
                       return reader.ReadToEnd();
                  }
                  else
                       return xmlFilePaths["Error"]; //TODO
             }
        }

         /// <summary>
         /// Retrieve the font requested from memory.
         /// </summary>
         /// <param name="key">The name of the file as specified by the LoadFont function.</param>
         /// <returns>The SpriteFont requested, else the default font.</returns>
        public static SpriteFont GetFont(string key)
        {
             if (fonts.ContainsKey(key))
                  return fonts[key];
             else
             {
                  bool didLoad = LoadFont(key, key);
                  if (didLoad)
                       return fonts[key];
                  else
                       return fonts["Default"]; //TODO
             }
        }

         /// <summary>
         /// Retrieve a font file from the hard disk.
         /// </summary>
         /// <param name="key">The name to call the file.</param>
         /// <param name="filename">The name of the file on disk.</param>
         /// <returns>True if the font was loaded correctly.</returns>
        public static bool LoadFont(string key, string filename)
        {
             if (fonts.ContainsKey(key))
                  return true;

             try
             {
                  fonts.Add(key, Content.Load<SpriteFont>("Fonts\\" + filename));
                  return true;
             }
             catch (Exception e)
             {
                  Utilities.ErrorManager.AddError(e);
                  return false;
             }
             
        }

         /// <summary>
         /// Set the fully content path for use with non supported file loading.
         /// </summary>
        private static void setDirectory()
        {
             contentFilePath = Directory.GetCurrentDirectory() + "\\Content\\";
        }
    }
}
